﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using System.Windows.Markup;
using System;
using Xceed.Silverlight.Compatibility;

namespace Xceed.Silverlight.DragDrop
{
  public class DraggedElementConfiguration : DependencyObject
  {
    #region DefaultElementTemplate Private Static Property

    private static DataTemplate DefaultElementTemplate
    {
      get
      {
        if( DefaultElementTemplateCache == null )
        {
          string imageTemplate = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:xcdragdrop='http://schemas.xceed.com/silverlight/xaml/controls' ><Image Source='{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(xcdragdrop:DragDropAdornerManager.DraggedElementImageSource)}'/></DataTemplate>";

          DefaultElementTemplateCache = CompatibilityUtils.XamlParse( imageTemplate ) as DataTemplate;
        }

        return DefaultElementTemplateCache;
      }
    }

    private static DataTemplate DefaultElementTemplateCache;

    #endregion

    #region ElementTemplate Public Property

    public static readonly DependencyProperty ElementTemplateProperty = DependencyProperty.Register(
      "ElementTemplate",
      typeof( DataTemplate ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( DraggedElementConfiguration.DefaultElementTemplate ) );

    public DataTemplate ElementTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( DraggedElementConfiguration.ElementTemplateProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.ElementTemplateProperty, value );
      }
    }

    #endregion

    #region DisplaySiblingElements Property

    public static readonly DependencyProperty DisplaySiblingElementsProperty = DependencyProperty.Register(
      "DisplaySiblingElements",
      typeof( bool ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( true ) );

    public bool DisplaySiblingElements
    {
      get
      {
        return ( bool )this.GetValue( DraggedElementConfiguration.DisplaySiblingElementsProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.DisplaySiblingElementsProperty, value );
      }
    }

    #endregion

    #region PositionAdornerRelativeToElement Property

    public static readonly DependencyProperty PositionAdornerRelativeToElementProperty = DependencyProperty.Register(
      "PositionAdornerRelativeToElement",
      typeof( bool ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( true ) );

    public bool PositionAdornerRelativeToElement
    {
      get
      {
        return ( bool )this.GetValue( DraggedElementConfiguration.PositionAdornerRelativeToElementProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.PositionAdornerRelativeToElementProperty, value );
      }
    }

    #endregion

    #region SiblingElementTemplate Public Property

    public static readonly DependencyProperty SiblingElementTemplateProperty = DependencyProperty.Register(
      "SiblingElementTemplate",
      typeof( DataTemplate ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( DraggedElementConfiguration.DefaultElementTemplate ) );

    public DataTemplate SiblingElementTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( DraggedElementConfiguration.SiblingElementTemplateProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.SiblingElementTemplateProperty, value );
      }
    }

    #endregion

    #region CanDropCursor Property

    public static readonly DependencyProperty CanDropCursorProperty =
        DependencyProperty.Register( "CanDropCursor", typeof( DragDropCursor ), typeof( DraggedElementConfiguration ), new PropertyMetadata( DragDropCursor.Arrow ) );


    public DragDropCursor CanDropCursor
    {
      get
      {
        return ( DragDropCursor )this.GetValue( DraggedElementConfiguration.CanDropCursorProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.CanDropCursorProperty, value );
      }
    }

    #endregion

    #region NoDropCursor Property

    public static readonly DependencyProperty NoDropCursorProperty =
        DependencyProperty.Register( "NoDropCursor", typeof( DragDropCursor ), typeof( DraggedElementConfiguration ), new PropertyMetadata( DragDropCursor.No ) );


    public DragDropCursor NoDropCursor
    {
      get
      {
        return ( DragDropCursor )this.GetValue( DraggedElementConfiguration.NoDropCursorProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.NoDropCursorProperty, value );
      }
    }

    #endregion

    #region CanDropCursorTemplate Property

    public static readonly DependencyProperty CanDropCursorTemplateProperty =
        DependencyProperty.Register( "CanDropCursorTemplate", typeof( DataTemplate ), typeof( DraggedElementConfiguration ), new PropertyMetadata( null ) );

    public DataTemplate CanDropCursorTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( DraggedElementConfiguration.CanDropCursorTemplateProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.CanDropCursorTemplateProperty, value );
      }
    }

    #endregion

    #region NoDropCursorTemplate Property

    public static readonly DependencyProperty NoDropCursorTemplateProperty =
        DependencyProperty.Register( "NoDropCursorTemplate", typeof( DataTemplate ), typeof( DraggedElementConfiguration ), new PropertyMetadata( null ) );

    public DataTemplate NoDropCursorTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( DraggedElementConfiguration.NoDropCursorTemplateProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.NoDropCursorTemplateProperty, value );
      }
    }

    #endregion

    #region ElementFadeInDuration Property

    public static readonly DependencyProperty ElementFadeInDurationProperty = DependencyProperty.Register(
      "ElementFadeInDuration",
      typeof( Nullable<TimeSpan> ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( null ) );

    public Nullable<TimeSpan> ElementFadeInDuration
    {
      get
      {
        return ( Nullable<TimeSpan> )this.GetValue( DraggedElementConfiguration.ElementFadeInDurationProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.ElementFadeInDurationProperty, value );
      }
    }

    #endregion

    #region ElementFadeOutDuration Property

    public static readonly DependencyProperty ElementFadeOutDurationProperty = DependencyProperty.Register(
      "ElementFadeOutDuration",
      typeof( Nullable<TimeSpan> ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( null ) );

    public Nullable<TimeSpan> ElementFadeOutDuration
    {
      get
      {
        return ( Nullable<TimeSpan> )this.GetValue( DraggedElementConfiguration.ElementFadeOutDurationProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.ElementFadeOutDurationProperty, value );
      }
    }

    #endregion

    #region ApplyDraggedElementRenderTransform Property

    public static readonly DependencyProperty ApplyDraggedElementRenderTransformProperty = DependencyProperty.Register(
      "ApplyDraggedElementRenderTransform",
      typeof( bool ),
      typeof( DraggedElementConfiguration ),
      new PropertyMetadata( ( bool )true ) );

    public bool ApplyDraggedElementRenderTransform
    {
      get
      {
        return ( bool )this.GetValue( DraggedElementConfiguration.ApplyDraggedElementRenderTransformProperty );
      }
      set
      {
        this.SetValue( DraggedElementConfiguration.ApplyDraggedElementRenderTransformProperty, value );
      }
    }

    #endregion

    #region Internal Methods

    internal TimeSpan GetDraggedElementFadeInDuration()
    {
      if( this.ElementFadeInDuration.HasValue )
      {
        return ( TimeSpan )this.ElementFadeInDuration.Value;
      }

      return TimeSpan.FromMilliseconds( 250 );
    }

    internal TimeSpan GetDraggedElementFadeOutDuration()
    {
      if( this.ElementFadeOutDuration.HasValue )
      {
        return ( TimeSpan )this.ElementFadeOutDuration.Value;
      }

      return TimeSpan.FromMilliseconds( 250 );
    }

    #endregion
  }
}
